#include <iostream>
#include "message.hpp"

// 测试消息类  -- 序列化, 反序列化, 类型的检查
void testMessage_rpc()
{
    using std::cout;
    using std::endl;
    //jsonrpc::BaseMessage::ptr prpc = jsonrpc::MessageFactory::create(jsonrpc::MType::REQ_RPC);
    //jsonrpc::RpcRequest::ptr prp = std::dynamic_pointer_cast<jsonrpc::RpcRequest>(Prpc);

    jsonrpc::RpcRequest::ptr pRpcReq = jsonrpc::MessageFactory::create<jsonrpc::RpcRequest>();

    // 设置正文
    Json::Value param;
    param["num1"] = 11;
    param["num2"] = 22;
    pRpcReq->setMethod("Add");
    pRpcReq->setparameters(param);

    // 序列化
    std::string req = pRpcReq->serialize();
    std::cout << req << std::endl;

    // 反序列化
    Json::Value tmp;
    jsonrpc::BaseMessage::ptr pRpcReq2 = jsonrpc::MessageFactory::create(jsonrpc::MType::REQ_RPC);
    bool ret = pRpcReq2->unserialize(req);
    if(ret == false)return;

    ret = pRpcReq2->check();
    if(ret == false)return;
    // 基类没有子类的方法, 需要强转一下(或使用参数包按类型去生产消息)
    jsonrpc::RpcRequest::ptr _pRpcReq2 =  std::dynamic_pointer_cast<jsonrpc::RpcRequest>(pRpcReq2);
    cout << _pRpcReq2->method() << endl;
    cout << _pRpcReq2->parameters()["num1"].asInt() << " " << _pRpcReq2->parameters()["num2"].asInt() << endl;
}   

void testMessage_topic()
{
    using std::cout;
    using std::endl;
    jsonrpc::TopicRequest::ptr ptop = jsonrpc::MessageFactory::create<jsonrpc::TopicRequest>();
    ptop->setTopicKey("News");
    ptop->setTopicMsg("新闻消息");
    ptop->setOptype(jsonrpc::TopicOptype::TOPIC_PUBLISH);
    std::string tmp = ptop->serialize();
    cout << tmp << endl;

    jsonrpc::BaseMessage::ptr ptop2 = jsonrpc::MessageFactory::create(jsonrpc::MType::REQ_TOPIC);
    bool ret = ptop2->unserialize(tmp);
    if(ret == false)return;
    ret = ptop2->check();
    if(ret == false)return;
    jsonrpc::TopicRequest::ptr _ptop = std::dynamic_pointer_cast<jsonrpc::TopicRequest>(ptop2);

    cout << _ptop->topicKey() << endl;
    cout << (int)_ptop->optype() << endl;
    cout << _ptop->topicMsg() << endl;
}

void testMessage_Service()
{
    using std::cout;
    using std::endl;
    jsonrpc::ServiceRequest::ptr p = jsonrpc::MessageFactory::create<jsonrpc::ServiceRequest>();
    p->setMethod("Add");
    p->setOptype(jsonrpc::ServiceOptype::SERVICE_REGISTRY);
    p->setHost(jsonrpc::Address("127.0.0.1", 9090));

    // 序列化
    std::string tmp = p->serialize();
    cout << tmp << endl;
    jsonrpc::BaseMessage::ptr p2 = jsonrpc::MessageFactory::create(jsonrpc::MType::REQ_SERVICE);
    // 反序列化
    bool ret = p2->unserialize(tmp);
    if(ret == false)return;
    ret = p2->check();
    if(ret == false)return;
    jsonrpc::ServiceRequest::ptr _p2 = std::dynamic_pointer_cast<jsonrpc::ServiceRequest>(p2);
    cout << _p2->method() << endl;
    cout << (int)_p2->optype() << endl;
    cout << _p2->host().first << endl;
    cout << _p2->host().second << endl;

}

void testMessage_rpc_rsp()
{
    using std::cout;
    using std::endl;
    jsonrpc::RpcResponse::ptr p = jsonrpc::MessageFactory::create<jsonrpc::RpcResponse>();

    // 设置正文
    Json::Value param;
    p->setRCode(jsonrpc::RCode::RCODE_OK);
    p->setResult(33);

    // 序列化
    std::string rsp = p->serialize();
    std::cout << rsp << std::endl;

    // 反序列化
    Json::Value tmp;
    jsonrpc::BaseMessage::ptr p2 = jsonrpc::MessageFactory::create(jsonrpc::MType::RSP_RPC);
    bool ret = p2->unserialize(rsp);
    if(ret == false)return;

    ret = p2->check();
    if(ret == false)return;
    // 基类没有子类的方法, 需要强转一下(或使用参数包按类型去生产消息)
    jsonrpc::RpcResponse::ptr _p2 =  std::dynamic_pointer_cast<jsonrpc::RpcResponse>(p2);
    cout << (int)_p2->rcode() << endl;
    cout << _p2->result().asInt() << endl;
}


void testMessage_topic_rsp()
{
    using std::cout;
    using std::endl;
    jsonrpc::TopicResponse::ptr p = jsonrpc::MessageFactory::create<jsonrpc::TopicResponse>();
    p->setRCode(jsonrpc::RCode::RCODE_OK);

    // 序列化
    std::string tmp = p->serialize();
    cout << tmp << endl;
    jsonrpc::BaseMessage::ptr p2 = jsonrpc::MessageFactory::create(jsonrpc::MType::RSP_TOPIC);
    bool ret = p2->unserialize(tmp);
    if(ret == false)return;
    ret = p2->check();
    if(ret == false)return;
    jsonrpc::TopicResponse::ptr _p2 = std::dynamic_pointer_cast<jsonrpc::TopicResponse>(p2);

    cout << (int)_p2->rcode() << endl;
}


void testMessage_serivce_rsp()
{
    using std::cout;
    using std::endl;
    jsonrpc::ServiceResponse::ptr p = jsonrpc::MessageFactory::create<jsonrpc::ServiceResponse>();
    p->setRCode(jsonrpc::RCode::RCODE_OK);
    p->setMethod("Add");
    p->setOptype(jsonrpc::ServiceOptype::SERVICE_DISCOVERY);

    std::vector<jsonrpc::Address> addrs;
    addrs.push_back(jsonrpc::Address("127.0.0.1", 9090));
    addrs.push_back(jsonrpc::Address("127.0.0.1", 9091));
    p->setHost(addrs);
    // 序列化
    std::string tmp = p->serialize();
    cout << tmp << endl;
    jsonrpc::BaseMessage::ptr p2 = jsonrpc::MessageFactory::create(jsonrpc::MType::RSP_SERVICE);
    // 反序列化
    bool ret = p2->unserialize(tmp);
    if(ret == false)return;
    ret = p2->check();
    if(ret == false)return;
    jsonrpc::ServiceResponse::ptr _p2 = std::dynamic_pointer_cast<jsonrpc::ServiceResponse>(p2);

    cout << (int)_p2->rcode() << endl;
    cout << (int)_p2->optype() << endl;
    cout << _p2->method() << endl;
    std::vector<jsonrpc::Address> addrs2 = _p2->hosts();
    for(auto& addr : addrs2)
    {
        cout << "ip " << addr.first << "  port " << addr.second << endl;
    }

}


// g++ -std=c++11 test.cpp -o test -ljsoncpp
int main()
{

    testMessage_serivce_rsp();
    return 0;
}

